package com.ruoyi.web.zbAdmin.controller;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.web.zbAdmin.controller.param.BaseUserQuery;
import com.ruoyi.web.zbAdmin.domain.CaiSystemLogs;
import com.ruoyi.web.zbAdmin.mapper.CaiBaseUserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统日志
 * @author liandyao
 * @date 2022/12/16 8:46
 */
@Api(tags="才能招标-系统访问日志")
@RestController
@RequestMapping("/zbApi/appLogs")
@Slf4j
public class CaiSystemLogsController {

    @Autowired
    private CaiBaseUserMapper userMapper;


    public static final String collectionName = "caiSystemLogs";

    /**
     * 获取用户列表
     */
    @ApiOperation("获取访问app日志信息列表")
    @GetMapping("/list")
    public TableDataInfo list(BaseUserQuery userQuery) {

        MongoTemplate mongoTemplate = this.userMapper.getMongoTemplate();

        Query query = new Query();
        if(StrUtil.isNotBlank(userQuery.getPhone())){
            query.addCriteria(Criteria.where("userPhone").is(userQuery.getPhone()));
        }

        // 设置起始页和每页查询条数
        Pageable pageable = PageRequest.of(userQuery.getCurPage(), userQuery.getPageSize(), Sort.by(Sort.Order.desc("date")));
        // 查询记录总数
        long totalCount = mongoTemplate.count(query,collectionName);
        //查询分页后的记录
        List<CaiSystemLogs> result = mongoTemplate.find(query.with(pageable), CaiSystemLogs.class, collectionName);

        return TableDataInfo.success(result,totalCount);
    }
}
